package com.haixiaoke.saas.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.model.OSSObject;
import com.haixiaoke.saas.common.config.WeChatConfig;
import com.haixiaoke.saas.common.constant.HttpStatus;
import com.haixiaoke.saas.common.constant.OrderConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.oss.OssService;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.weChat.domain.RefundOrder;
import com.haixiaoke.saas.common.weChat.service.WeChatService;
import com.haixiaoke.saas.department.domain.WechatAccount;
import com.haixiaoke.saas.department.service.IWechatAccountService;
import com.haixiaoke.saas.order.domain.CheckOutSubOrder;
import com.haixiaoke.saas.order.mapper.CheckOutSubOrderMapper;
import com.haixiaoke.saas.order.service.ICheckOutSubOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 退房订单子级订单退款Service业务层处理
 * 
 * @author panda
 * @date 2024-04-30
 */
@Service
public class CheckOutSubOrderServiceImpl implements ICheckOutSubOrderService {
    @Autowired
    private CheckOutSubOrderMapper checkOutSubOrderMapper;
    @Autowired
    private WeChatConfig weChatConfig;
    @Autowired
    private IWechatAccountService wechatAccountService;
    @Autowired
    private OssService ossService;
    @Autowired
    private WeChatService weChatService;

    /**
     * 查询退房订单子级订单退款
     * 
     * @param checkOutOrderId 退房订单子级订单退款主键
     * @return 退房订单子级订单退款
     */
    @Override
    public CheckOutSubOrder selectCheckOutSubOrderByCheckOutOrderId(String checkOutOrderId) {
        return checkOutSubOrderMapper.selectCheckOutSubOrderByCheckOutOrderId(checkOutOrderId);
    }

    /**
     * 查询退房订单子级订单退款列表
     * 
     * @param checkOutSubOrder 退房订单子级订单退款
     * @return 退房订单子级订单退款
     */
    @Override
    public List<CheckOutSubOrder> selectCheckOutSubOrderList(CheckOutSubOrder checkOutSubOrder) {
        return checkOutSubOrderMapper.selectCheckOutSubOrderList(checkOutSubOrder);
    }

    /**
     * 新增退房订单子级订单退款
     * 
     * @param checkOutSubOrder 退房订单子级订单退款
     * @return 结果
     */
    @Override
    public int insertCheckOutSubOrder(CheckOutSubOrder checkOutSubOrder) {
        checkOutSubOrder.setCreateTime(DateUtils.getNowDate());
        return checkOutSubOrderMapper.insertCheckOutSubOrder(checkOutSubOrder);
    }

    /**
     * 修改退房订单子级订单退款
     * 
     * @param checkOutSubOrder 退房订单子级订单退款
     * @return 结果
     */
    @Override
    public int updateCheckOutSubOrder(CheckOutSubOrder checkOutSubOrder) {
        checkOutSubOrder.setUpdateTime(DateUtils.getNowDate());
        return checkOutSubOrderMapper.updateCheckOutSubOrder(checkOutSubOrder);
    }

    /**
     * 批量删除退房订单子级订单退款
     * 
     * @param checkOutOrderIds 需要删除的退房订单子级订单退款主键
     * @return 结果
     */
    @Override
    public int deleteCheckOutSubOrderByCheckOutOrderIds(String[] checkOutOrderIds) {
        return checkOutSubOrderMapper.deleteCheckOutSubOrderByCheckOutOrderIds(checkOutOrderIds);
    }

    /**
     * 删除退房订单子级订单退款信息
     * 
     * @param checkOutOrderId 退房订单子级订单退款主键
     * @return 结果
     */
    @Override
    public int deleteCheckOutSubOrderByCheckOutOrderId(String checkOutOrderId) {
        return checkOutSubOrderMapper.deleteCheckOutSubOrderByCheckOutOrderId(checkOutOrderId);
    }

    @Override
    public int insertBatchCheckOutSubOrder(List<CheckOutSubOrder> checkOutSubOrderList) {
        return checkOutSubOrderMapper.insertBatchCheckOutSubOrder(checkOutSubOrderList);
    }

    @Override
    public AjaxResult refundCheckOutSubOrder(CheckOutSubOrder checkOutSubOrder) {

        CheckOutSubOrder subOrder = checkOutSubOrderMapper.selectCheckOutSubOrderByCheckOutOrderIdAndOrderId(checkOutSubOrder.getCheckOutOrderId(),checkOutSubOrder.getOrderId());

        if (!StrUtil.equals(OrderConstants.ORDER_STATE_REFUND_EXCEPTION,subOrder.getRefundOrderState())) {
            return AjaxResult.error("订单状态异常，不可再次退款");
        }

        WechatAccount account = wechatAccountService.selectWechatAccountByTenantId(checkOutSubOrder.getTenantId());
        if (ObjectUtil.isEmpty(account)) {
            throw new ServiceException("商户号数据异常");
        }
        OSSObject object = ossService.getObject(account.getAccountCertificate());
        byte[] bytes = ossService.parseObject(object);
        if (bytes == null) {
            throw new ServiceException("商户证书获取异常");
        }

        RefundOrder refundOrder = new RefundOrder();
        refundOrder.setAppId(weChatConfig.getAppId());
        refundOrder.setMchId(account.getAccountId());
        refundOrder.setMchApiKey(account.getAccountApiKey());

        refundOrder.setOutTradeNo(subOrder.getOrderId());
        refundOrder.setOutRefundNo(subOrder.getOrderId());
/*        BigDecimal add = currentOrder.getDeposit().add(currentOrder.getTotalRent());
        BigDecimal currentRefund = BigDecimal.ZERO;
        if (add.compareTo(totalPrice) > 0) {
            currentRefund = add.subtract(totalPrice);
        } else if (add.compareTo(totalPrice) == 0) {
            currentRefund = add;
        } else if (add.compareTo(totalPrice) < 0) {
            currentRefund = totalPrice.subtract(add);
        }
        int refund = currentRefund.multiply(BigDecimal.valueOf(100)).intValue();*/
//        int refund = subOrder.getRefundAmount().multiply(BigDecimal.valueOf(100)).intValue();
        refundOrder.setTotalFee(1/*refund*/);
        refundOrder.setRefundFee(1/*refund*/);
        refundOrder.setNotifyUrl(weChatConfig.getCheckOutCallBackUrl());
        refundOrder.setMchCertificate(bytes);
        AjaxResult ajaxResult = weChatService.refundOrder(refundOrder);
        if (HttpStatus.SUCCESS == (int) ajaxResult.get("code")) {
            checkOutSubOrder.setRefundOrderState(OrderConstants.ORDER_STATE_REFUND_SUCCESSFUL);
        } else {
            checkOutSubOrder.setRefundOrderState(OrderConstants.ORDER_STATE_REFUND_EXCEPTION);
        }
        checkOutSubOrderMapper.updateCheckOutSubOrder(subOrder);
        return ajaxResult;
    }

    @Override
    public CheckOutSubOrder selectCheckOutSubOrderByOrderId(String orderId) {
        return checkOutSubOrderMapper.selectCheckOutSubOrderByOrderId(orderId);
    }

    @Override
    public boolean checkOrderState(String checkOutOrderId) {
        int result = checkOutSubOrderMapper.checkOrderState(checkOutOrderId);
        return result > 0;
    }
}
